package com.yaozheng.vocationaltraining.service.impl.limited;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.IOnlineExamView;
import com.yaozheng.vocationaltraining.iview.limited.ITimedPaperView;
import com.yaozheng.vocationaltraining.service.OnlineExamService;
import com.yaozheng.vocationaltraining.service.limited.TimedPaperService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;

import java.util.HashMap;

/**
 * 极限挑战答案服务
 */
@EBean
public class TimedAnswerServiceImpl implements OnlineExamService, ITimedPaperView {
    IOnlineExamView iOnlineExamView;
    @Bean(TimedPaperServiceImpl.class)
    TimedPaperService timedPaperService;

    @Override
    public void init(IOnlineExamView iOnlineExamView) {
        this.iOnlineExamView = iOnlineExamView;
        timedPaperService.init(this);
    }

    @Override
    public void getQuestionList(int paperId) {

        timedPaperService.createPapper(paperId);
    }

    @Background
    @Override
    public void submitAnswers(int paperId, String answers) {
        try {
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("id", paperId);
//            jsonObject.put("answers", answers);
            OkHttpClientManager.getInstance().postJson(
                    Constants.ROOTURL + "/exam/race/" + paperId,
                    iOnlineExamView.getToken(), answers,
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iOnlineExamView) {

                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
//                            JSONObject resultJsonObject = TypeUtils.getJsonObject(jsonObject,
//                                    "result");
                            iOnlineExamView.submitAnswersSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iOnlineExamView.submitAnswersError(error);
                        }


                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iOnlineExamView.isResponseResult()) {
                iOnlineExamView
                        .submitAnswersError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Override
    public void loadSaiKuangInfo(int gameId) {

    }

    @Override
    public void createPapperSuccess(JSONObject jsonObject) {
        iOnlineExamView.responseSuccess(jsonObject);
    }

    @Override
    public void createPapperError(String error) {
        iOnlineExamView.responseError(error);
    }

    @Override
    public String getToken() {
        return iOnlineExamView.getToken();
    }

    @Override
    public boolean isResponseResult() {
        return iOnlineExamView.isResponseResult();
    }

    @Override
    public void tokenFail() {
        iOnlineExamView.tokenFail();
    }
}
